<!doctype html>
<html lang="en">
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>言成言成啊</title>
    <link rel="shortcut icon" href="/images/favicon.ico">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/css/meethigher-font.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/css/animate.css">
    <link rel="stylesheet" href="/css/index.css">
</head>
<body>
<header class="header">
    <div class="header-menu"><span class="fa fa-bars"></span></div>
    <div class="header-search"><span class="fa fa-search"></span></div>
    <div class="header-title"><a href="/">言成言成啊 | <span class="header-subtitle">Kit Chen&#39;s Blog</span></a></div>
    <ul class="header-navbar clearFix">
        
            <li><a href="/">主页</a></li>
        
            <li><a href="/tags">标签</a></li>
        
            <li><a href="/archives">归档</a></li>
        
            <li><a href="/search">导航</a></li>
        
    </ul>
</header>
<main class="main">
    <div class="main-content meethigher-scroll">
    <article class="post">
        <div class="post-title">
            <h2 class="title">Git本地仓库入门教程</h2>
        </div>
        <div class="post-media2">
            <span class="post-time">2019-10-03</span>
            
                <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/git/">git</a></li></ul>
            
        </div>
        <div class="post-content blog-markdown">
            <blockquote>
<p>详细教程：参考<a href="https://www.liaoxuefeng.com/wiki/896043488029600" target="_blank" rel="noopener">https://www.liaoxuefeng.com/wiki/896043488029600</a></p>
</blockquote>
<h3 id><a href="#" class="headerlink" title></a></h3><h4 id="第一步-初始化本地仓库"><a href="#第一步-初始化本地仓库" class="headerlink" title="第一步 初始化本地仓库"></a>第一步 初始化本地仓库</h4><p><code>git init</code>初始化一个本地仓库</p>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git init</span><br><span class="line">Initialized empty Git repository <span class="keyword">in</span> <span class="string">C:</span><span class="regexp">/Users/</span>kitchen<span class="regexp">/Desktop/</span>Theme<span class="regexp">/.git/</span></span><br></pre></td></tr></table></figure>

<hr>
<h4 id="第二步-将文件添加到版本库"><a href="#第二步-将文件添加到版本库" class="headerlink" title="第二步 将文件添加到版本库"></a>第二步 将文件添加到版本库</h4><ol>
<li><code>git add &lt;filename&gt;</code>将文件添加到版本库</li>
<li><code>git commit -m &lt;message&gt;</code>完成，<message>最好是写，而且是<code>英文</code>！</message></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">$ git add test.txt</span><br><span class="line"></span><br><span class="line">$ git status</span><br><span class="line">On branch master</span><br><span class="line"></span><br><span class="line">No commits yet</span><br><span class="line"></span><br><span class="line">Changes to be committed:</span><br><span class="line">  (<span class="keyword">use</span> <span class="string">"git rm --cached &lt;file&gt;..."</span> <span class="keyword">to</span> unstage)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">new</span> <span class="keyword">file</span>:   test.txt</span><br><span class="line"></span><br><span class="line">$ git <span class="keyword">commit</span> -m <span class="string">"first commit"</span></span><br><span class="line">[<span class="keyword">master</span> (root-<span class="keyword">commit</span>) <span class="number">42</span>ce0e6] <span class="keyword">first</span> <span class="keyword">commit</span></span><br><span class="line"> <span class="number">1</span> <span class="keyword">file</span> <span class="keyword">changed</span>, <span class="number">1</span> insertion(+)</span><br><span class="line"> <span class="keyword">create</span> <span class="keyword">mode</span> <span class="number">100644</span> test.txt</span><br></pre></td></tr></table></figure>

<hr>
<h4 id="第三步-随时掌握工作区状态"><a href="#第三步-随时掌握工作区状态" class="headerlink" title="第三步 随时掌握工作区状态"></a>第三步 随时掌握工作区状态</h4><p>如果修改了项目内容，可以通过<code>git status</code>来查看工作区的状态</p>
<p>如果<code>git status</code> 告诉你文件被修改过，用过<code>git diff &lt;filename&gt;</code>可以查看修改内容</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">$ git status</span><br><span class="line">On branch master</span><br><span class="line">Changes not staged for <span class="keyword">commit</span>:</span><br><span class="line">  (<span class="keyword">use</span> <span class="string">"git add &lt;file&gt;..."</span> <span class="keyword">to</span> <span class="keyword">update</span> what will be committed)</span><br><span class="line">  (<span class="keyword">use</span> <span class="string">"git checkout -- &lt;file&gt;..."</span> <span class="keyword">to</span> discard changes <span class="keyword">in</span> working <span class="keyword">directory</span>)</span><br><span class="line"></span><br><span class="line">        modified:   test.txt</span><br><span class="line"></span><br><span class="line"><span class="keyword">no</span> changes added <span class="keyword">to</span> <span class="keyword">commit</span> (<span class="keyword">use</span> <span class="string">"git add"</span> <span class="keyword">and</span>/<span class="keyword">or</span> <span class="string">"git commit -a"</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">$ git diff test.txt</span><br><span class="line">diff <span class="comment">--git a/test.txt b/test.txt</span></span><br><span class="line"><span class="keyword">index</span> a1b78e2..aab983c <span class="number">100644</span></span><br><span class="line"><span class="comment">--- a/test.txt</span></span><br><span class="line">+++ b/test.txt</span><br><span class="line">@@ <span class="number">-1</span> +<span class="number">1</span>,<span class="number">2</span> @@</span><br><span class="line">-<span class="keyword">first</span> <span class="keyword">commit</span></span><br><span class="line">\ <span class="keyword">No</span> <span class="keyword">newline</span> <span class="keyword">at</span> <span class="keyword">end</span> <span class="keyword">of</span> <span class="keyword">file</span></span><br><span class="line">+<span class="keyword">first</span> <span class="keyword">commit</span></span><br><span class="line">+<span class="keyword">second</span> <span class="keyword">commit</span></span><br><span class="line">\ <span class="keyword">No</span> <span class="keyword">newline</span> <span class="keyword">at</span> <span class="keyword">end</span> <span class="keyword">of</span> <span class="keyword">file</span></span><br></pre></td></tr></table></figure>

<hr>
<h4 id="第四步-版本回退"><a href="#第四步-版本回退" class="headerlink" title="第四步 版本回退"></a>第四步 版本回退</h4><p>可以通过<code>git log</code>查看<strong><em>最近到远</em></strong>的版本日志</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">$ git log</span><br><span class="line"><span class="keyword">commit</span> f6af78dc6dc5944e0e04df3ac46b83ecc667a3c4 (<span class="keyword">HEAD</span> -&gt; <span class="keyword">master</span>)</span><br><span class="line">Author: meethigher &lt;meethigher@qq.com&gt;</span><br><span class="line"><span class="built_in">Date</span>:   Thu <span class="keyword">Oct</span> <span class="number">3</span> <span class="number">18</span>:<span class="number">18</span>:<span class="number">28</span> <span class="number">2019</span> +<span class="number">0800</span></span><br><span class="line"></span><br><span class="line">    third <span class="keyword">commit</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">commit</span> <span class="number">535</span>bf16545a58f9e8c6f9d29218e5c46befa9a67</span><br><span class="line">Author: meethigher &lt;meethigher@qq.com&gt;</span><br><span class="line"><span class="built_in">Date</span>:   Thu <span class="keyword">Oct</span> <span class="number">3</span> <span class="number">18</span>:<span class="number">17</span>:<span class="number">25</span> <span class="number">2019</span> +<span class="number">0800</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">second</span> <span class="keyword">commit</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">commit</span> <span class="number">5</span>d8a7ff0c255d26df63e675393a85c756e4230ce</span><br><span class="line">Author: meethigher &lt;meethigher@qq.com&gt;</span><br><span class="line"><span class="built_in">Date</span>:   Thu <span class="keyword">Oct</span> <span class="number">3</span> <span class="number">18</span>:<span class="number">07</span>:<span class="number">23</span> <span class="number">2019</span> +<span class="number">0800</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">first</span> <span class="keyword">commit</span></span><br></pre></td></tr></table></figure>

<p>如果想要只输出文件版本号<head><meta name="generator" content="Hexo 3.9.0">跟注释，可以通过<code>git log --pretty=oneline</code>查看</head></p>
<figure class="highlight livecodeserver"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ git <span class="built_in">log</span> <span class="comment">--pretty=oneline</span></span><br><span class="line">f6af78dc6dc5944e0e04df3ac46b83ecc667a3c4 (HEAD -&gt; master) <span class="keyword">third</span> commit</span><br><span class="line"><span class="number">535</span>bf16545a58f9e8c6f9d29218e5c46befa9a67 <span class="keyword">second</span> commit</span><br><span class="line"><span class="number">5</span>d8a7ff0c255d26df63e675393a85c756e4230ce <span class="keyword">first</span> commit</span><br></pre></td></tr></table></figure>

<p>现在的位置是在third commit，如果想要回退到first commit，可以通过<code>git reset --hard &lt;HEAD&gt;</code></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git <span class="keyword">reset</span> <span class="comment">--hard 5d8a7ff0c255d26df63e675393a85c756e4230ce</span></span><br><span class="line"><span class="keyword">HEAD</span> <span class="keyword">is</span> <span class="keyword">now</span> <span class="keyword">at</span> <span class="number">5</span>d8a7ff <span class="keyword">first</span> <span class="keyword">commit</span></span><br></pre></td></tr></table></figure>

<p>可以看到现在已经回退到了first commit</p>
<p>但是，此时如果再用<code>git log</code>会发现之后的最新的那几个版本已经看不到了！</p>
<p>这就好比你从21世纪穿梭到19世纪，回不去了。</p>
<p>解决办法：</p>
<ol>
<li>命令行没有清空的情况下，可以找之前的版本号</li>
<li>命令行清空的情况下，可以通过<code>git reflog</code>来找版本号，<code>git reflog</code>用来记录你的每一次命令</li>
</ol>
<figure class="highlight perl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$ git reflog</span><br><span class="line"><span class="number">5</span>d8a7ff (HEAD -&gt; master) HEAD@&#123;0&#125;: <span class="keyword">reset</span>: moving to <span class="number">5</span>d8a7ff0c255d26df63e675393a85c756e4230ce</span><br><span class="line">f6af78d HEAD@&#123;1&#125;: commit: third commit</span><br><span class="line"><span class="number">535</span>bf16 HEAD@&#123;2&#125;: commit: second commit</span><br><span class="line"><span class="number">5</span>d8a7ff (HEAD -&gt; master) HEAD@&#123;3&#125;: commit (initial): first commit</span><br></pre></td></tr></table></figure>

<p>牛批，我又找到版本号了！</p>
<h3 id="-1"><a href="#-1" class="headerlink" title></a></h3>
        </div>
        
            <div class="busuanzi">
                <span id="busuanzi_container_page_pv">阅读量<span id="busuanzi_value_page_pv"></span>次</span>
                <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            </div>
        
    </article>
</div>
    <pointer href="javascript:void(0);" class="arrow fa fa-arrow-up"></pointer>
<pointer href="javascript:void(0);" class="arrow fa fa-arrow-down"></pointer>

</main>
<aside class="aside">
    <div class="aside-content">
        <div class="aside-avatar"><img src="/images/kitchen.jpg" alt="言成"></div>
        <div class="aside-author">言成</div>
        <div class="aside-description">爱学习，爱运动，爱看妹子</div>
        <ul class="aside-contact clearFix">
            <li><span class="fa fa-github"></span><a href="https://github.com/meethigher">Github</a></li>
            <li><span class="fa fa-envelope"></span><a href="javascript:void(0);">meethigher@qq.com</a></li>
            <li><span class="fa fa-weixin"></span><a href="javascript:void(0);"><img src="/images/weixin.png" alt="扫描微信二维码，加我哦！"></a></li>
        </ul>
    </div>
</aside>
<div class="toolbar"><span class="fa fa-th"></span></div>
<div id="particles-js"></div>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/particles.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/app.js"></script>
<script src="/js/meethigher.js"></script>
<script src="/js/toolbar.js"></script>
</body>
</html>